#include "new_dynamic.h"
#include <QPainter>
#include <QStyleOption>
#include <QMessageBox>

new_dynamic::new_dynamic(QWidget *parent) : QWidget(parent)
{
    /* 1.实例化 */
    layout = new QVBoxLayout;
    btnLay = new QVBoxLayout;
    buttomLay = new QHBoxLayout;
    textEdit=new QTextEdit;
    picLabel=new QLabel();
    img=new QImage; //新建一个image对象
    cancel = new QPushButton("取消");
    ok = new QPushButton("发布");
    add = new QPushButton("添加图片");

    /* 2.设置对象名称 */
    cancel->setObjectName("cancel");
    ok->setObjectName("ok");
    add->setObjectName("add");
    textEdit->setObjectName("textEdit");

    /* 3.布局 */
    btnLay->addStretch();
    btnLay->addWidget(ok);
    btnLay->addWidget(cancel);
    btnLay->addStretch();
    btnLay->addWidget(add);
    buttomLay->addWidget(picLabel);
    buttomLay->addStretch();
    buttomLay->addLayout(btnLay);
    layout->addWidget(textEdit);
    layout->addLayout(buttomLay);
    layout->addSpacing(1);
    this->setLayout(layout);
    this->resize(500,400);
    textEdit->resize(400,300);

    /* 4 加载样式表文件 default.css */
    QFile fCss(":/qss/qss/new_dynamic.css");
    if( !fCss.open(QFile::ReadOnly)){
        qDebug("css File load false");
        return;
    }
    QString strCssContent(fCss.readAll());
    setStyleSheet(strCssContent);
    fCss.close();

    /* 5.设置标题 */
    this->setWindowTitle("新键动态");
    QIcon icon(":/img/img/time.ico");
    this->setWindowIcon(icon);
    photo.load(":/img/img/photo.png");

    /* 6.关联信号 */
    connect(this->ok,SIGNAL(clicked(bool)),this,SLOT(onBtnOkClicked(bool)));
    connect(this->add,SIGNAL(clicked(bool)),this,SLOT(onBtnAddClicked(bool)));
    connect(this->cancel,SIGNAL(clicked(bool)),this,SLOT(onBtnCancelClicked(bool)));
}

void new_dynamic::paintEvent(QPaintEvent *event)
{
    //qDebug()<<"paintEvent() called";
    this->textEdit->resize(this->width()*0.92,this->height()*0.46);
    this->picLabel->resize(this->height()*1.2,this->height()*0.45);
    QPixmap img2=photo.scaled(picLabel->size(),Qt::KeepAspectRatio);
    picLabel->setPixmap(img2);

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void new_dynamic::onBtnOkClicked(bool on)
{
    if(this->textEdit->toPlainText()==NULL){
        QMessageBox::warning(this, tr("警告"),tr("内容不能为空."),QMessageBox::Cancel);
        return;
    }
    releaseDynamic(textEdit->toPlainText(),file_name);
    emit onNewDynamic(textEdit->toPlainText(),file_name);
    QMessageBox::information(this, "提示", "由于服务器带宽较小,需要等上大概40s才能传输完成\n", QMessageBox::Ok, QMessageBox::Ok);
    onBtnCancelClicked(true);
}

void new_dynamic::onBtnCancelClicked(bool on)
{
    this->textEdit->clear();
    this->picLabel->clear();
    this->hide();
}

void new_dynamic::onBtnAddClicked(bool on)
{
    file_name=QFileDialog::getOpenFileName(this,"open","../","*.jpg");
    if(file_name.isEmpty()){
        return ;
    }
    picLabel->resize(300,260);
    photo.load(file_name);
    QPixmap img2=photo.scaled(picLabel->size(),Qt::KeepAspectRatio);
    picLabel->setPixmap(img2);
}

